import { VerticalBox, HorizontalBox, Button, ListView, ProgressIndicator } from "std-widgets.slint";

export struct PackageInfo {
    name: string,
    version: string,
    pkg-type: string,
    installed: bool,
}

export global MainEvents {
    callback install-requested(pkg-type: string, path: string);
    callback notify-progress(progress: float);
    callback show-error(message: string);
}

export component MainWindow inherits Window {
    in-out property <[PackageInfo]> packages;
    in-out property <float> install-progress: 0.0;

    VerticalBox {
        HorizontalBox {
            spacing: 10px;
            Button { text: "玲珑包"; style: primary; }
            Button { text: "Flatpak"; }
            Button { text: "Snap"; }
        }
        
        ListView {
            for pkg in root.packages: HorizontalBox {
                width: 100%;
                spacing: 8px;
                Text { text: pkg.name; horizontal-alignment: left; }
                Text { text: pkg.version; }
                Button {
                    text: pkg.installed ? "卸载" : "安装";
                    enabled: !root.install-progress > 0;
                    clicked => {
                        if (!pkg.installed) {
                            root.install-requested(pkg.pkg-type, "");
                        }
                    }
                }
            }
        }
        
        ProgressIndicator {
            visible: root.install-progress > 0;
            percentage: root.install-progress;
        }
    }
}